package cn.terminus.web.customer.contractselldefyloaninfo.controller;

import cn.terminus.web.customer.contractselldefyloaninfo.form.ContractSellDefyLoanInfoNewForm;
import cn.terminus.web.customer.contractselldefyloaninfo.form.ContractSellDefyLoanInfoEditForm;
import cn.terminus.web.customer.contractselldefyloaninfo.form.ContractSellDefyLoanInfoQueryForm;
import cn.terminus.web.customer.contractselldefyloaninfo.form.ContractSellDefyLoanInfoDeleteForm;
import cn.terminus.web.customer.contractselldefyloaninfo.form.ContractSellDefyLoanInfoGetsForm;
import cn.terminus.client.customer.contractselldefyloaninfo.param.ContractSellDefyLoanInfoNewParam;
import cn.terminus.client.customer.contractselldefyloaninfo.param.ContractSellDefyLoanInfoEditParam;
import cn.terminus.client.customer.contractselldefyloaninfo.param.ContractSellDefyLoanInfoQueryParam;
import cn.terminus.client.customer.contractselldefyloaninfo.param.ContractSellDefyLoanInfoDeleteParam;
import cn.terminus.client.customer.contractselldefyloaninfo.param.ContractSellDefyLoanInfoGetsParam;
import cn.terminus.client.customer.contractselldefyloaninfo.result.ContractSellDefyLoanInfoResult;
import cn.terminus.client.customer.contractselldefyloaninfo.service.ContractSellDefyLoanInfoService;
import cn.terminus.result.common.result.TerminusResult;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.web.customer.common.AbstractTerminusController;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.result.common.exception.ServiceException;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.terminus.result.common.util.BeanCopyUtils;
import cn.terminus.result.common.context.TerminusCallContext;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;

/**
 * 贷款违约(ContractSellDefyLoanInfo)控制层
 *
 * @author makejava
 * @version 1.0.0
 * @apiDefine ContractSellDefyLoanInfo 贷款违约
 * @since 2021-07-21 11:18:16
 */
@RestController
// todo 请修改path
@RequestMapping("/contractSellDefyLoanInfo")
@Api(value = "贷款违约(ContractSellDefyLoanInfo)控制层")
public class ContractSellDefyLoanInfoController extends AbstractTerminusController {
    /**
     * 服务对象
     */
    @Resource
    private ContractSellDefyLoanInfoService contractSellDefyLoanInfoService;

    /**
     * @api {POST} /contractSellDefyLoanInfo/add 贷款违约新增
     * @apiVersion 1.0.0
     * @apiGroup ContractSellDefyLoanInfo
     * @apiName 贷款违约新增
     * @apiDescription 贷款违约新增
     * @apiParam (RequestBody) {Long} contractId  合同ID
     * @apiParam (RequestBody) {Integer} loanDefyWithinDay  付款逾期多少日内
     * @apiParam (RequestBody) {String} loanDefyWithinDayUnitCode  付款逾期(内)日期类型
     * @apiSuccess (Success 200) {String} code    状态码（00000000表示请求成功）
     * @apiSuccess (Success 200) {Boolean} success    成功
     * @apiSuccess (Success 200) {String} message    结果消息
     * @apiSuccess (Success 200) {String} data    结果
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":"[]"}
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "贷款违约新增", httpMethod = "POST")
    public TerminusResult<String> add(@RequestBody ContractSellDefyLoanInfoNewForm form) {
        ContractSellDefyLoanInfoNewParam param = BeanCopyUtils.copyProperties(form, ContractSellDefyLoanInfoNewParam.class);
        try {
            return contractSellDefyLoanInfoService.add(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("新增贷款违约失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("新增贷款违约失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    /**
     * @api {POST} /contractSellDefyLoanInfo/edit 贷款违约编辑
     * @apiVersion 1.0.0
     * @apiGroup ContractSellDefyLoanInfo
     * @apiName 贷款违约编辑
     * @apiDescription 贷款违约编辑
     * @apiParam (RequestBody) {Long} id  ID
     * @apiParam (RequestBody) {Long} contractId  合同ID
     * @apiParam (RequestBody) {Integer} loanDefyWithinDay  付款逾期多少日内
     * @apiParam (RequestBody) {String} loanDefyWithinDayUnitCode  付款逾期(内)日期类型
     * @apiSuccess (Success 200) {String} code    状态码（00000000表示请求成功）
     * @apiSuccess (Success 200) {Boolean} success    成功
     * @apiSuccess (Success 200) {String} message    结果消息
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":true}
     */

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "贷款违约编辑", httpMethod = "POST")
    public TerminusResult<Boolean> edit(@RequestBody ContractSellDefyLoanInfoEditForm form) {
        ContractSellDefyLoanInfoEditParam param = BeanCopyUtils.copyProperties(form, ContractSellDefyLoanInfoEditParam.class);
        try {
            return contractSellDefyLoanInfoService.update(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("修改贷款违约失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("修改贷款违约失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    /**
     * @api {POST} /contractSellDefyLoanInfo/delete 贷款违约删除
     * @apiVersion 1.0.0
     * @apiGroup ContractSellDefyLoanInfo
     * @apiName 贷款违约删除
     * @apiDescription 贷款违约删除
     * @apiParam (RequestBody) {String[]} ids  贷款违约ids
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":true}
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "贷款违约删除", httpMethod = "POST")
    public TerminusResult<Boolean> delete(@RequestBody ContractSellDefyLoanInfoDeleteForm form) {
        ContractSellDefyLoanInfoDeleteParam param = BeanCopyUtils.copyProperties(form, ContractSellDefyLoanInfoDeleteParam.class);
        try {
            return contractSellDefyLoanInfoService.delete(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("删除贷款违约失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("删除贷款违约失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    /**
     * @api {POST} /contractSellDefyLoanInfo/gets 贷款违约详情
     * @apiVersion 1.0.0
     * @apiGroup ContractSellDefyLoanInfo
     * @apiName 贷款违约详情
     * @apiDescription 贷款违约详情
     * @apiParam (RequestBody) {String[]} ids  贷款违约ids
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccess (Success 200) {Object[]} data.datas    贷款违约列表
     * @apiSuccess (Success 200) {Long} data.datas.id  ID
     * @apiSuccess (Success 200) {Long} data.datas.contractId  合同ID
     * @apiSuccess (Success 200) {Integer} data.datas.loanDefyWithinDay  付款逾期多少日内
     * @apiSuccess (Success 200) {String} data.datas.loanDefyWithinDayUnitCode  付款逾期(内)日期类型
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":[]}
     */
    @RequestMapping(value = "/gets", method = RequestMethod.POST)
    @ApiOperation(value = "贷款违约详情查询", httpMethod = "POST")
    public TerminusResult<List<ContractSellDefyLoanInfoResult>> gets(@RequestBody ContractSellDefyLoanInfoGetsForm form) {
        ContractSellDefyLoanInfoGetsParam param = BeanCopyUtils.copyProperties(form, ContractSellDefyLoanInfoGetsParam.class);
        try {
            return contractSellDefyLoanInfoService.gets(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("查询贷款违约失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询贷款违约失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    /**
     * @api {POST} /contractSellDefyLoanInfo/query 贷款违约查询
     * @apiVersion 1.0.0
     * @apiGroup ContractSellDefyLoanInfo
     * @apiName 贷款违约查询
     * @apiDescription 贷款违约查询
     * @apiParam (RequestBody) {Long} contractId  合同ID
     * @apiParam (RequestBody) {Integer} loanDefyWithinDay  付款逾期多少日内
     * @apiParam (RequestBody) {String} loanDefyWithinDayUnitCode  付款逾期(内)日期类型
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccess (Success 200) {Object[]} data.datas    贷款违约列表
     * @apiSuccess (Success 200) {Long} data.datas.id  ID
     * @apiSuccess (Success 200) {Long} data.datas.contractId  合同ID
     * @apiSuccess (Success 200) {Integer} data.datas.loanDefyWithinDay  付款逾期多少日内
     * @apiSuccess (Success 200) {String} data.datas.loanDefyWithinDayUnitCode  付款逾期(内)日期类型
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":[]}
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiOperation(value = "贷款违约查询", httpMethod = "POST")
    public TerminusResult<List<ContractSellDefyLoanInfoResult>> query(@RequestBody ContractSellDefyLoanInfoQueryForm form) {
        ContractSellDefyLoanInfoQueryParam param = BeanCopyUtils.copyProperties(form, ContractSellDefyLoanInfoQueryParam.class);
        try {
            return contractSellDefyLoanInfoService.query(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("查询贷款违约失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询贷款违约失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    /**
     * @api {POST} /contractSellDefyLoanInfo/page 贷款违约分页查询
     * @apiVersion 1.0.0
     * @apiGroup ContractSellDefyLoanInfo
     * @apiName 贷款违约分页查询
     * @apiDescription 贷款违约分页查询
     * @apiParam (RequestBody) {Long} contractId  合同ID
     * @apiParam (RequestBody) {Integer} loanDefyWithinDay  付款逾期多少日内
     * @apiParam (RequestBody) {String} loanDefyWithinDayUnitCode  付款逾期(内)日期类型
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccess (Success 200) {long} data.pageSize    分页大小
     * @apiSuccess (Success 200) {long} data.currentPage    当前页
     * @apiSuccess (Success 200) {long} data.totalPages    总页数
     * @apiSuccess (Success 200) {long} data.totalRows    总记录数
     * @apiSuccess (Success 200) {long} data.minRowNumber    当页起始行号
     * @apiSuccess (Success 200) {long} data.maxRowNumber    当页最大行号
     * @apiSuccess (Success 200) {Object[]} data.datas    贷款违约列表
     * @apiSuccess (Success 200) {Long} data.datas.id  ID
     * @apiSuccess (Success 200) {Long} data.datas.contractId  合同ID
     * @apiSuccess (Success 200) {Integer} data.datas.loanDefyWithinDay  付款逾期多少日内
     * @apiSuccess (Success 200) {String} data.datas.loanDefyWithinDayUnitCode  付款逾期(内)日期类型
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":[]}
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "贷款违约分页查询", httpMethod = "POST")
    public TerminusResult<TerminusPageAble<ContractSellDefyLoanInfoResult>> page(@RequestBody ContractSellDefyLoanInfoQueryForm form) {
        ContractSellDefyLoanInfoQueryParam param = BeanCopyUtils.copyProperties(form, ContractSellDefyLoanInfoQueryParam.class);
        try {
            TerminusPageInfo page = new TerminusPageInfo(form.getCurrentPage(), form.getRowsOfPage());
            return contractSellDefyLoanInfoService.page(param, page, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("分页查询贷款违约失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("分页查询贷款违约失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

}
